package com.awifi.demo.netty.nio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/9/1 14:49
 */
public class NettyServer {

    public static void main(String[] args){

        int port = 8080;                                   //监听端口号为8080
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            System.out.println("时间服务器监听端口：" + port);
            Socket socket = null;

            while (true) {
                socket = server.accept();                           //accecpt阻塞等待连接

                //有客户端连接进来则创建处理线程，负责和客户端交互
                new Thread(new EchoServerHandler(socket)).start();
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if (server != null) {
                System.out.println("服务器端关闭。");
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                server = null;
            }
        }
    }

}
